Ext.define('Admin.view.supplier.SupplierController', {

    extend: 'Admin.ux.GridController',

    alias: 'controller.supplier',

    //编辑行
    editAction: function (grid, rowIndex, colIndex, item, e, record, row) {

        var rec = grid.getStore().getAt(rowIndex);

        var me = this;

        var win = Ext.create("Admin.ux.Window", {
            title: '审核信息',
            width: 710,
            height: 650,
            animateTarget: row,
            items: { xtype: 'supplier-form' },
            buttons: [{
                text: '取消',
                ui: 'gray',
                iconCls: 'x-fa 	fa-ban',
                handler: function (btn) {
                    btn.up("window").close();
                }
            }, {
                text: '不通过',
                ui: 'gray',
                iconCls: 'x-fa 	fa-ban',
                handler: function (btn) {
                    me.errorWin(row);
                }
            }, {
                text: '审核通过',
                ui: 'gray',
                iconCls: 'x-fa 	fa-ban',
                handler: function (btn) {
                    me.auditWin(row);
                }
            }],

            listeners: {
                show: function (w, opts) {
                }
            }
        });

    },

    //弹出确认框
    errorWin: function (row) {
        var win = Ext.create("Admin.ux.Window", {
            title: '采购部门//退回到的信息',
            width: 450,
            height: 350,
            animateTarget: row,
            items: { xtype: 'supplier-submit' },
            buttons: [{
                text: '确定',
                ui: 'gray',
                iconCls: 'x-fa 	fa-ban',
                handler: function (btn) {
                    btn.up("window").close();
                }
            }, {
                text: '取消',
                ui: 'gray',
                iconCls: 'x-fa 	fa-ban',
                handler: function (btn) {
                    btn.up("window").close();
                }
            }]
        });
    },
    //弹出确认框
    auditWin: function (row) {
        var win = Ext.create("Admin.ux.Window", {
            title: '采购部门//提交到的信息',
            width: 450,
            height: 250,
            animateTarget: row,
            items: { xtype: 'supplier-submit' },
            buttons: [{
                text: '确定',
                ui: 'gray',
                iconCls: 'x-fa 	fa-ban',
                handler: function (btn) {
                    btn.up("window").close();
                }
            }, {
                text: '取消',
                ui: 'gray',
                iconCls: 'x-fa 	fa-ban',
                handler: function (btn) {
                    btn.up("window").close();
                }
            }]
        });
    },
    //新增
    feedbackAction: function(b,opts) {

        var me = this;
        var form = b.up("form").getForm();
        var viewModel = me.getViewModel();
        form.submit({
            clientValidation: true,
            url:'/supplier/feedback',
            params:form.getFieldValues(),
            success:function(form1,action){
                var grid = form.down('grid');//寻找grid
                var store = grid.getStore();
                store.loadData(action.result.data.feedbacklist);
            },
            failure:function(form,action){
                switch (action.failureType) {
                    case Ext.form.action.Action.CLIENT_INVALID:
                        Ext.create('Admin.ux.Action').error('客户端验证不通过');
                        break;
                    default:
                        viewModel.set("error",action.result.msg+"!");
                }
            }
        });
    },
    moreAction: function (grid, rowIndex, colIndex, item, e, record, row) {

        var rec = grid.getStore().getAt(rowIndex);

        var me = this;

        var treeMenu = new Ext.menu.Menu({
            shadow: "drop",
            allowOtherMenus: true,
            items: [
                new Ext.menu.Item({
                    text: "审核信息",
                    iconCls: 'array-grid-buy-col',
                    handler: function () {
                        var win = Ext.create("Admin.ux.Window", {
                            title: '审核信息',
                            width: 900,
                            height: 700,
                            animateTarget: row,
                            items: { xtype: 'supplier-form' },
                            buttons: [
                                {
                                    text: '审核通过',
                                    ui: 'soft-green',
                                    iconCls: 'x-fa 	fa-floppy-o',
                                    handler: function (btn) {
                                        var form = btn.up("window").down("form").getForm();
                                        Ext.create('Admin.ux.Action').submit(form, '/supplier/examinepass', me.getView(), win);
                                    }
                                },
                                {
                                    text: '审核不通过',
                                    ui: 'soft-green',
                                    iconCls: 'x-fa 	fa-floppy-o',
                                    handler: function (btn) {
                                        var form = btn.up("window").down("form").getForm();
                                        Ext.create('Admin.ux.Action').submit(form, '/supplier/examinenopass', me.getView(), win);
                                    }
                                },
                                {
                                    text: '重置',
                                    ui: 'soft-red',
                                    iconCls: 'x-fa 	fa-mail-reply',
                                    handler: function (btn) {
                                        var form = btn.up("window").down("form");
                                        form.getForm().load({
                                            url: '/supplier/get',
                                            waitMsg: '加载中...',
                                            params: {
                                                id: rec.get('id')
                                            },
                                            success: function () {
                                                //form.down('textfield[id=id]').setReadOnly(true);
                                                var grid = win.down('grid');//寻找grid
                                                var store = grid.getStore();
                                                store.loadData(action.result.data.feedbacklist);
                                            },
                                            failure: function (form, action) {
                                                if (action.result.msg != undefined) {
                                                    Ext.Msg.alert("提示", "<font color=red>" + action.result.msg + "!</font>");
                                                } else {
                                                    Ext.Msg.alert("提示", "<font color=red>加载出错,请刷新页面重试!</font>");
                                                }
                                            }
                                        });
                                        form.loadRecord(rec);
                                    }
                                },
                                {
                                    text: '取消',
                                    ui: 'gray',
                                    iconCls: 'x-fa 	fa-ban',
                                    handler: function (btn) {
                                        btn.up("window").close();
                                    }
                                }
                            ],

                            listeners: {
                                show: function (w, opts) {

                                    var form = this.down('form');

                                    form.getForm().load({
                                        url: '/supplier/get',
                                        waitMsg: '加载中...',
                                        params: {
                                            id: rec.get('deptid')
                                        },
                                        success: function (form,action) {
                                            //alert(action);
                                            //form.down('textfield[name=deptid]').setReadOnly(true);
                                            var grid = win.down('grid');//寻找grid
                                            var store = grid.getStore();
                                            store.loadData(action.result.data.feedbacklist);
                                        },
                                        failure: function (form, action) {
                                            if (action.result.msg != undefined) {
                                                Ext.Msg.alert("提示", "<font color=red>" + action.result.msg + "!</font>");
                                            } else {
                                                Ext.Msg.alert("提示", "<font color=red>加载出错,请刷新页面重试!</font>");
                                            }
                                        }
                                    });
                                }
                            }
                        });
                    }
                })
            ]
        });
        treeMenu.showAt(e.getPoint());
    }


});